#include <cstdio>
#include <cstring>

int n, a[ 301 ][ 301 ], c[ 90001 ];

int lowbit( int t )
{
    return t & ( t ^ ( t - 1 ) );
}

int sum( int x )
{
    int ret = 0;
    while ( x >= 1 )
    {
        ret += c[ x ];
        x -= lowbit( x );
    }
    return ret;
}

void add( int x )
{
    while ( x <= n * n )
    {
        c[ x ]++;
        x += lowbit( x );
    }
}

void init( )
{
    int i, j, x, y;
    for ( i = 0; i < n; i++ )
        for ( j = 0; j < n; j++ )
        {
            scanf("%d", &a[ i ][ j ]);
            if ( !a[ i ][ j ] )
            {
                x = i;
                y = j;
            }
        }
    for ( i = x + 1; i < n; i++ )
        a[ i - 1 ][ y ] = a[ i ][ y ];
    for ( i = y + 1; i < n; i++ )
        a[ n - 1 ][ i - 1 ] = a[ n - 1 ][ i ];
    a[ n - 1 ][ n - 1 ] = 0;
    memset( c, 0, sizeof( c ) );
}

void work( )
{
    int ret = 0, i, j;
    for ( i = 0; i < n * n - 1; i++ )
    {
        ret += i - sum( a[ i / n ][ i % n ] );
        add( a[ i / n ][ i % n ] );
    }
    if ( ret & 1 )
        puts("NO");
    else
        puts("YES");
}

int main( )
{
    while ( scanf("%d", &n) && n )
    {
        init( );
        work( );
    }
    return 0;
}
